<?php

class HerogroupDAO extends DAO {

    public static function getherogroups($uid = 0)
	{
		$uid = intval($uid);
        if (!$uid) return false;
        
        $mck = "hrg:{$uid}";
        $mcconf = self::mchash("herogroup", $uid);
        $ret = self::getcache($uid, $mcconf, $mck, $db, __FUNCTION__);
        if ($ret)
		{
            set_global("mc:{$mck}", $ret);
            return $ret;
        }
        $dbconf = self::dbhash($uid, "pvn_herogroup", "herogroup");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where uid={$uid}";
        $res = self::dbquery($dbconn, $sql);
		$ret = array();
        if ($res && mysql_num_rows($res) >= 1)
		{
            while ($row = mysql_fetch_assoc($res))
            {
                $row["grid"] = json_decode($row["grid"], true);
                $ret[$row["gid"]] = $row;
            }
            unset($row);
        }
		elseif($res) $ret = array();
		else $ret = false;

        self::dbclose($dbconn);
        if ($ret === false) return false;
        
        if (self::setcache($uid, $mcconf, $mck, $ret, __FUNCTION__)) set_global("mc:{$mck}", $ret);
        return $ret;
	}

	public static function getherogroup($uid = 0, $gid = 0)
	{
		$uid = intval($uid);
		$gid = intval($gid);
        if (!$uid || !$gid) return false;
        
        $mck = "hrg:{$uid}";
        $mcconf = self::mchash("herogroup", $uid);
        $ret = self::getcache($uid, $mcconf, $mck, $db, __FUNCTION__);
        if ($ret && $ret[$gid])
		{
            return $ret[$gid];
        }
        $dbconf = self::dbhash($uid, "pvn_herogroup", "herogroup");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where uid={$uid} and gid={$gid}";
        $ret = self::dbquery($dbconn, $sql, "row");

        self::dbclose($dbconn);
        if ($ret === false) return false;

		$ret["grid"] = json_decode($ret["grid"], true);
        
		self::delcache($uid, $mcconf, $mck, __FUNCTION__);
        return $ret;
	}

	public static function setherogroup($uid = 0, $gid = 0, $data = array())
	{
		$uid = intval($uid);
		$gid = intval($gid);
		if (!$uid || !$gid || !$data || !is_array($data)) return false;
    
        $ou = self::getherogroup($uid, $gid);
        if (!$ou) return false;

		$mck = "hrg:{$uid}";
        $mcconf = self::mchash("herogroup", $uid);
        if (!self::delcache($uid, $mcconf, $mck, __FUNCTION__)) return false;
        
        $sets = array();
		foreach($data as $k => $v)
		{
			switch($k)
			{
				case 'stat':
				case 'lv':
					$v = intval($v);
					$sets[] = "{$k}='{$v}'";
					break;
				case 'grid':
					$v = mysql_escape_string(json_encode($v));
					$sets[] = "{$k}='{$v}'";
					break;
			}
		}
		if(!$sets) return false;
		$sets = implode(",", $sets);
        
		$dbconf = self::dbhash($uid, "pvn_herogroup", "herogroup");
		$dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if (!$dbconn) return false;
		
		$sql = "update {$dbconf[4]} set {$sets} where uid={$uid} and gid={$gid}";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);
		
		return $ret;
	}

	public static function newherogroup($uid = 0, $gid = 0, $data = array())
	{
		$uid = intval($uid);
		$gid = intval($gid);
        if (!$uid || !$gid || !$data || !is_array($data)) return false;

		$ou = self::getherogroup($uid, $gid);
		if(is_array($ou))
		{
			$mck = "hrg:{$uid}";
			$mcconf = self::mchash("herogroup", $uid);
			if (!self::delcache($uid, $mcconf, $mck, __FUNCTION__)) return false;
		}

		$sets = array();
		foreach($data as $k => $v)
		{
			switch($k)
			{
				case 'stat':
				case 'lv':
					$v = intval($v);
					$sets[] = "{$k}='{$v}'";
					break;
				case 'grid':
					$v = mysql_escape_string(json_encode($v));
					$sets[] = "{$k}='{$v}'";
					break;
			}
		}
		if(!$sets) return false;
		$sets[] = "uid='{$uid}'";
		$sets[] = "gid='{$gid}'";
		$sets = implode(",", $sets);
        
        $dbconf = self::dbhash($uid, "pvn_herogroup", "herogroup");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if(!$dbconn) return false;

        $ret = self::dbquery($dbconn, "insert into {$dbconf[4]} set {$sets}");
        self::dbclose($dbconn);
        
        return intval($ret);
	}

	public static function setactiveherogroup($uid = 0, $gid = 0)
	{
		$uid = intval($uid);
		$gid = intval($gid);
        if (!$uid || !$gid) return false;

		$ou = self::getherogroup($uid, $gid);
        if (!$ou) return false;

		$mck = "hrg:{$uid}";
        $mcconf = self::mchash("herogroup", $uid);
        if (!self::delcache($uid, $mcconf, $mck, __FUNCTION__)) return false;

        $dbconf = self::dbhash($uid, "pvn_herogroup", "herogroup");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if(!$dbconn) return false;

        $ret = self::dbquery($dbconn, "update {$dbconf[4]} set stat=0 where uid={$uid} and gid!={$gid}");
		if($ret) $ret = self::dbquery($dbconn, "update {$dbconf[4]} set stat=1 where uid={$uid} and gid={$gid}");
        self::dbclose($dbconn);
        
        return $ret;
	}

	public static function delherogroup($uid = 0, $gid = 0)
	{
		$uid = intval($uid);
		$gid = intval($gid);
		if (!$uid || !$gid) return false;
    
        $ou = self::getherogroup($uid, $gid);
        if (!$ou) return false;

		$mck = "hrg:{$uid}";
        $mcconf = self::mchash("herogroup", $uid);
        if (!self::delcache($uid, $mcconf, $mck, __FUNCTION__)) return false;
        
		$dbconf = self::dbhash($uid, "pvn_herogroup", "herogroup");
		$dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if (!$dbconn) return false;
		
		$sql = "delete from {$dbconf[4]} where uid={$uid} and gid={$gid}";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);
		
		return $ret;
	}
}

?>